package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.DicType;
import com.jframe.cfg.model.Iframe;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.model.Tablemapping;
import com.jframe.cfg.service.DictypeService;
import com.jframe.cfg.service.IframeService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.cfg.service.TablemapService;
import com.jframe.sso.model.UserGroup;
import com.jframe.sso.service.UserGroupService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.*;
import com.jframe.sys.orm.utils.ResultSet;
import com.jframe.sys.utils.*;
import com.jframe.sys.webui.*;

public class IframeAction extends ExtendAction{

	private static String ACTION="iframeAction";
	private static String SERVICE="iframeService";
	private static Class<Iframe> clazz=Iframe.class;
	
	public void list(){
		String groupId = this.pageInfo.getPageTabId(request);
		List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
		List<Columns> commonColumns = new ArrayList<Columns>();
		frozenColumns.add(new Columns("ck","",40,""));
		frozenColumns.add(new Columns("id", "编号", 80,""));
		frozenColumns.add(new Columns("title", "页签名", 150,""));
		commonColumns.add(new Columns("url","链接",230,""));
		commonColumns.add(new Columns("tabId","tabId值",100,""));
		commonColumns.add(new Columns("menuCode","父菜单",120,new Editor("combobox",true),TabmCache.getComboboxJson("sm_sys_menu_code")));
		commonColumns.add(new Columns("typeId","页签类型",120,new Editor("combobox",true),DicCache.getComboboxJson("dm_sys_iframetype")));
		commonColumns.add(new Columns("dicCode","页签字典",120,new Editor("combobox",true),this.dictype()));
		commonColumns.add(new Columns("seq","顺序",80,""));
		commonColumns.add(new Columns("active","有效否",80,new Editor("combobox"),DicCache.getComboboxJson("dm_sys_active")));
		Grid grid = new Grid();
		grid.setUrl(ACTION+".do?method=load&groupId="+groupId);
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.request.setAttribute("groupId", groupId);
		this.forward("/jframe/cfg/list/iframeList.jsp");
	}
	
	public void load(){
		int start = this.pageInfo.getGridStart(request);
		int limit = this.pageInfo.getGridLimit(request);
		String conditions = StringUtil.null2Space(request.getParameter("conditions"));
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		IframeService iframeService = (IframeService)this.getBean("iframeService");	
		QueryHelp queryHelp = new QueryHelp(conditions,Iframe.class);
		ResultSet<Iframe> rs=iframeService.findListByPage(queryHelp," and groupId=? ","",groupId,start, limit);
		this.print(JsonUtil.obj2Json(rs));
	}
	
	public void save(){
		this.baseSave(SERVICE, clazz);
	}
	
	public void delete(){
		this.baseDelete(SERVICE);
	}
	
	public void getObjById(){
		this.print(JsonUtil.obj2Json(this.getObject(SERVICE)));
	}
	
	public List<UserGroup> getUserGroups(){
		UserGroupService userGroupService = (UserGroupService)this.getBean("userGroupService");
		List<UserGroup> ls = userGroupService.findList("where active=1 order by id");
		return ls;
	}
	
	public void getIframeType(){
		this.print(DicCache.getComboboxJson("dm_sys_iframetype"));
	}
	
	public void getDicType(){
		this.print(this.dictype());
	}
	
	private String dictype(){
		String json="[";
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");
		TablemapService tablemapService = (TablemapService)this.getBean("tablemapService");
		List<DicType> diclist= dictypeService.findAll();
		List<Tablemapping> maplist = tablemapService.findAll();
		for(DicType dictype:diclist){
			json +="{\"id\":\""+dictype.getEnname()+"\",\"text\":\""+dictype.getCnname()+"\"},";
		}
		for(Tablemapping maptype:maplist){
			json +="{\"id\":\""+maptype.getEnname()+"\",\"text\":\""+maptype.getCnname()+"\"},";
		}
		if(json.length()>3){
			json = json.substring(0,json.length()-1);
		}
		json +="]";
		return json;
	}

	public void getsysMenu(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		int id=0;
		List<Sysmenu> ls = sysmenuService.findListByUpIdAndGroupId(id,groupId);
		String json = "[";
		for(Sysmenu menu:ls){
			json +="{\"id\":\""+menu.getCode()+"\",";
			json +="\"text\":\""+menu.getText()+"\"";
			if(menu.getLeaf()==0){		
				json+=",\"state\":\"closed\",";
				json+="\"children\":"+this.getChildTree(menu.getId(), groupId);
			}
			json+="},";
		}
		//去除尾部逗号
		if(ls.size()>0){
			json = json.substring(0,json.length()-1);
		}
		json += "]";
		this.print(json);
	}
	
	private String getChildTree(int upId,int groupId){
		SysmenuService sysmenuService = (SysmenuService)this.getBean("sysmenuService");
		List<Sysmenu> ls = sysmenuService. findListByUpIdAndGroupId(upId,groupId);
		String json = "[";
		for(Sysmenu menu:ls){
			json +="{\"id\":\""+menu.getCode()+"\",";
			json +="\"text\":\""+menu.getText()+"\"";
			if(menu.getLeaf()==0){
				json+=",\"state\":\"closed\",";
				json+="\"children\":"+this.getChildTree(menu.getId(), groupId);
			}
			json+="},";
		}
		//去除尾部逗号
		if(ls.size()>0){
			json = json.substring(0,json.length()-1);
		}
		json += "]";
		return json;
	}
	
	public void getRightTrees(){
		int groupId = StringUtil.parseInt(this.request.getParameter("groupId"));
		SysmenuService sysmenuService = (SysmenuService)SpringUtil.getBean("sysmenuService");
		String code = StringUtil.null2Space(this.request.getParameter("id"));
		Sysmenu sysmenu = sysmenuService.load("code", code);
		int id=1;
		if(sysmenu.getId()>0){
			id=sysmenu.getId();
		}
		List<Sysmenu> treels = sysmenuService.findListByUpIdAndGroupId(id, groupId);
		List<Tree> ls = new ArrayList<Tree>();
		for(Sysmenu menu:treels){
			Tree tree = new Tree();
			tree.setId(menu.getCode());
			tree.setIconCls(menu.getIconCls());
			tree.setText(menu.getText());
			if(menu.getLeaf()==0){
				tree.setState("closed");
			}
			ls.add(tree);
		}
		this.print(JsonUtil.list2Json(ls));
	}
	
	public void getIframes(){
		String code = StringUtil.null2Space(request.getParameter("code"));		
		IframeService iframeService = (IframeService)this.getBean("iframeService");
		List<Iframe> iframes = iframeService.findByProperties(code);
		this.print(JsonUtil.list2Json(iframes));
	}
	
	public void loadDic(){
		String code = StringUtil.null2Space(request.getParameter("code"));
		this.print(DicCache.getComboboxJson(code));
	}
	
	public void loadTabm(){
		String code = StringUtil.null2Space(request.getParameter("code"));	
		this.print(TabmCache.getComboboxJson(code));
	}
	
	public void getCombotreeDicType(){
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		String json = TabmCache.getCombotreeJson("sm_sys_app");
			List<Tree> ls = JsonUtil.json2List(json, Tree.class);
			for(Tree tree:ls){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("id", "root"+tree.getId());
				map.put("text", tree.getText());
				List<Map<String,Object>> children = this.getChildren(tree.getId());
				if(children.size()>0){
					map.put("children", children);
					map.put("state", "closed");
				}
				maps.add(map);
			}
			this.print(JsonUtil.list2Json(maps));
	}
	
	public List<Map<String,Object>> getChildren(String upId){
		List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
		DictypeService dictypeService = (DictypeService)this.getBean("dictypeService");
		List<DicType> dicTypes = dictypeService.findListByUpIds(upId);
		TablemapService tablemapService = (TablemapService)this.getBean("tablemapService");		
		List<Tablemapping> Tablemappings = tablemapService.findListByUpIds(upId);
		for(DicType dicType:dicTypes){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", dicType.getEnname());
			map.put("text", dicType.getCnname());
			maps.add(map);
		}
		for(Tablemapping Tablemapping : Tablemappings){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", Tablemapping.getEnname());
			map.put("text", Tablemapping.getCnname());
			maps.add(map);
		}
		return maps;
	}
}
